package erwiki.ui.app.admin.dialogs.hierachychange;

import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.elwiki_data.WikiPage;

/**
 * Провайдер коонтента для TreeViewer.</br>
 * Входные данные - WikiPage, т.е. дерево потомков заданной страницы.
 * 
 * @author vfedorov
 */
class ViewContentProvider implements IStructuredContentProvider, ITreeContentProvider {
	private static final long serialVersionUID = 2886744569195233750L;

	@Override
	public void inputChanged(Viewer v, Object oldInput, Object newInput) {
	}

	@Override
	public void dispose() {
	}

	@Override
	public Object[] getElements(Object parent) {
		Object[] result;
		if (parent instanceof List) {
			result = ((List<?>) parent).toArray();
		} else if (parent instanceof WikiPage) {
			result = ((WikiPage) parent).getChildren().toArray();
		} else {
			result = new Object[0];
			Assert.isTrue(false, ":FVK: Internal error.");
		}
		return result;
	}

	@Override
	public Object getParent(Object child) {
		if (child instanceof WikiPage) {
			return ((WikiPage) child).getParent();
		}
		return null;
	}

	@Override
	public Object[] getChildren(Object parent) {
		if (parent instanceof WikiPage) {
			return ((WikiPage) parent).getChildren().toArray();
		}
		return new Object[0];
	}

	@Override
	public boolean hasChildren(Object parent) {
		if (parent instanceof WikiPage) {
			return ((WikiPage) parent).getChildren().size() > 0;
		}
		return false;
	}
}
